const app = getApp();

Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        recommend: {
            questions: [],
            resource: [],
            notes: []
        },
        loadModal: true
    },
    onLoad: function (options) {

    },
    onShow: function () {
        this.setData({
            recommend: null,
            loadModal: true
        })
        this.getRecommend();
        this.tabBar();
    },
    tabBar(){
        if(typeof this.getTabBar === 'function' && this.getTabBar()){
            this.getTabBar().setData({
                selected:1
            })
        }
    },
    search: function () {
        let that = this;
        // if (that.data.search == null)
        if (that.data.search != null && that.data.search!=''&& that.data.search!=undefined){
            that.setData({
                question: null
            });
            wx.request({
                url: app.globalData.serverUrl + "question/searchByKeyword/",
                method: 'post',
                data: {
                    "keyword": that.data.search
                },
                success: function (res){
                    let data = res.data.data;
                    // console.log(data)
                    if (data.status == 200) {
                        console.log(data.data)
                        that.setData({
                            'recommend.questions': data.data.questionList,
                            'recommend.resource': data.data.resourceList,
                            'recommend.notes': data.data.noteList
                        });
                    }
                }
            })
        } else {
            that.onShow();
        }
    },
    getRecommend: function () {
        let that = this;
        wx.request({
            url: app.globalData.serverUrl + 'recommend',
            method: 'get',
            success: function (res) {
                if (res.data.status == 200) {
                    that.setData({
                        recommend : res.data.data,
                        loadModal: false
                    });
                }
                console.log(res.data.data)
            }
        })
    },
    onEditorReady: function (option){
        let id = option.currentTarget.dataset.id;
        wx.createSelectorQuery().select('#detail'+id).context(res => {
            this.editorCtx = res.context;
            this.editorCtx.setContents({
                html: option.currentTarget.dataset.detail,
                success: res => {
                    console.log('[setContents success]')
                }
            })
        }).exec();
    },
    questionDetail:function (e) {
        let id = e.currentTarget.dataset.id;
        console.log(e.currentTarget.dataset)
        wx.navigateTo({
            url: '../question-detail/question-detail?id='+id,
        });
    },
    resourceDetail: function (e) {
        let id = e.currentTarget.dataset.id;
        console.log(e.currentTarget.dataset)
        // wx.navigateTo({
        //     url: '../question-detail/question-detail?id='+id,
        // });
    },
    noteDetail: function (e) {
        let id = e.currentTarget.dataset.id;
        console.log(e.currentTarget.dataset)
        // wx.navigateTo({
        //     url: '../question-detail/question-detail?id='+id,
        // });
    }
});
